package com.springboot.demo.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.springboot.demo.entity.*;
import com.springboot.demo.entity.validate.MapCoordinateGroup;
import com.springboot.demo.entity.validate.user.IdGroup;
import com.springboot.demo.mapper.MapMapper;
import com.springboot.demo.service.MapService;
import com.springboot.demo.service.UserService;
import com.springboot.demo.utils.ResponseUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.groups.Default;
import java.util.List;

/**
 * @author wubc683
 * @desc
 * @date 2021-11-18 14:14
 */
@RestController
@Api(tags = "地图接口")
@Validated
public class MapController {
    @Autowired
    ResponseUtils responseUtils;
    @Autowired
    MapService mapService;

    @GetMapping("/map/monitor")
    @ApiOperation(value = "获取监控地图信息")
    @ApiOperationSupport(order = 10)
    public ResponseUtils<MapData> getMonitorMap() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        MapData monitorMap = mapService.getMonitorMap(implUser.getId());
        return responseUtils.buildSuccessResponse(monitorMap);
    }

    @GetMapping("/map/monitor/scenes")
    @ApiOperation(value = "获取监控地图的场景列表", notes = "场景中的设备信息通过场景id取获取，取第一个设备的坐标给场景,地图显示所有场景位置" +
            "点击场景，显示/隐藏该场景的配置的设备，并且用线连起来，场景和设备都有状态，用不同的图标显示")
    @ApiOperationSupport(order = 20)
    public ResponseUtils<List<Scene>> getMapScenes(@RequestParam(value = "search", required = false)
                                                   @ApiParam("搜索，输入：场景名称/序列号,1~32位") String search,
                                                   @Valid PageRequest pageRequest) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        PageResult mapScenes = mapService.getMapScenes(implUser.getId(), search, pageRequest);
        return responseUtils.buildSuccessResponse(mapScenes);
    }

    @PutMapping("/map/monitor")
    @ApiOperation(value = "修改地图状态信息", notes = "离开地图页面时调用")
    @ApiOperationSupport(order = 30, includeParameters = {"mapData.id", "mapData.color", "mapData.disperseType", "mapData" +
            ".longitude", "mapData.latitude", "mapData.scale"})
    public ResponseUtils updateMonitorMap(@Validated({IdGroup.class, MapCoordinateGroup.class, Default.class})
                                          @RequestBody MapData mapData) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        mapService.updateMonitorMap(implUser.getId(), mapData);
        return responseUtils.buildSuccessResponse();
    }

    @PutMapping("/map/monitor/position")
    @ApiOperation(value = "将当前位置设为起始位置")
    @ApiOperationSupport(order = 40, includeParameters = {"mapData.id", "mapData" +
            ".longitude", "mapData.latitude", "mapData.scale"})
    public ResponseUtils updateMonitorMapPosition(@Validated({IdGroup.class, MapCoordinateGroup.class})
                                                  @RequestBody MapData mapData) {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        UserDetailsImpl userDetailsImpl = (UserDetailsImpl) principal;
        User implUser = userDetailsImpl.getUser();
        mapService.updateMonitorMapPosition(implUser.getId(), mapData);
        return responseUtils.buildSuccessResponse();
    }
}
